home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1991 / 02 / malloc.asm < prev    next >
Assembly Source File  |  1990-11-01  |  1KB  |  93 lines

  1.     title    malloc
  2.     include    asm.inc
  3.  
  4.     public    calloc
  5.     public    free
  6.     public    malloc
  7.  
  8.     .const
  9. ertx_malloc_range    db    'malloc out of range',0
  10.  
  11.     .code
  12.     extn    ms_dos_strerror,set_strerror
  13.  
  14.  
  15. ;;    calloc
  16. ;
  17. ;    entry    CX    requested byte count (1..FFF0)
  18. ;    exit    ES:DI    storage pointer
  19. ;        AX    actual byte count
  20. ;        Cf    if no storage
  21. ;
  22. calloc    proc
  23.     call    malloc            ; allocate storage
  24.     jc    cal1
  25.  
  26.     pushm    ax,cx,di        ; zero storage
  27.     mov    cx,ax
  28.     movx    ax,0
  29.     shr    cx,1
  30.     rep    stosw
  31.     popm    di,cx,ax
  32. cal1:    ret
  33. calloc    endp
  34.  
  35.  
  36. ;;    free
  37. ;
  38. ;    entry    ES:DI    storage pointer (OK if NULL)
  39. ;    exit    ES:DI    NULL
  40. ;        Cf    if bad pointer
  41. ;    uses    AX
  42. ;
  43. free    proc
  44.     mov    ax,es
  45.     or    ax,di
  46.     jz    fee1            ;\ if NULL pointer - ignore call
  47.  
  48.     mov    ah,49h
  49.     call    ms_dos_strerror
  50.  
  51.     mov    di,NULL_POINTER        ; return NULL (don't modify flags)
  52.     mov    es,di
  53.  
  54. fee1:    ret
  55. free    endp
  56.  
  57.  
  58. ;;    malloc
  59. ;
  60. ;    entry    CX    requested byte count (1..FFF0)
  61. ;    exit    ES:DI    storage pointer
  62. ;        AX    actual byte count
  63. ;        Cf    if no storage
  64. ;
  65. malloc    proc
  66.     jcxz    mal2            ; if byte count out of range
  67.     mov    ax,cx
  68.     add    ax,15
  69.     jc    mal2            ; if byte count out of range
  70.     and    al,0F0h
  71.  
  72.     pushm    ax,bx            ; convert byte count to paragraph cnt
  73.     shr    ax,1
  74.     shr    ax,1
  75.     shr    ax,1
  76.     shr    ax,1
  77.     mov    bx,ax
  78.  
  79.     mov    ah,48h            ; request memory from dos
  80.     call    ms_dos_strerror
  81.     jc    mal1            ;  if not enough memory
  82.     mov    es,ax
  83.     movx    di,NULL_POINTER
  84.  
  85. mal1:    popm    bx,ax
  86.     ret
  87.  
  88. mal2:    lea    ax,ertx_malloc_range
  89.     jmp    set_strerror
  90. malloc    endp
  91.  
  92.     end
  93.